package com.wzy.oa_sys.service.impl.log;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.oa_sys.mapper.log.LogMapper;
import com.wzy.oa_sys.mapper.user.UserMapper;
import com.wzy.oa_sys.model.pojo.log.OaLog;
import com.wzy.oa_sys.model.request.log.LogReq;
import com.wzy.oa_sys.model.response.log.LogResp;
import com.wzy.oa_sys.service.log.LogService;
import com.wzy.oa_sys.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, OaLog> implements LogService {
    @Autowired
    private LogMapper logMapper;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void addLog(LogReq logReq,long userId) {
        OaLog oaLog = new OaLog();
        oaLog.setCMethodName(logReq.getMn());
        oaLog.setCOpName(logReq.getOpn());
        oaLog.setCResult(logReq.getRes());
        oaLog.setDtTime(LocalDateTime.now());
        oaLog.setNUserId(userId);
        logMapper.insert(oaLog);
    }

    @Override
    public List<LogResp> getLogList(HttpServletRequest request) {
        Long userId = jwtUtil.extractUserId(request);
        String role = jwtUtil.extractUserRole(request);
        System.out.println("role: "+role);
        List<OaLog> oaLogs = logMapper.selectList(null);
        List<OaLog> oaLogList = oaLogs.stream().sorted(Comparator.comparing(OaLog::getDtTime).reversed()).collect(Collectors.toList());
        List<LogResp> logResps = new ArrayList<>();
        if(role.contains("SUPERMAN")){
            oaLogList.forEach(oa -> {
                LogResp build = LogResp.builder()
                        .methodname(oa.getCMethodName())
                        .opname(oa.getCOpName())
                        .time(oa.getDtTime())
                        .build();
                if(userMapper.selectUserById(oa.getNUserId())!=null){
                    build.setUsername(userMapper.selectUserById(oa.getNUserId()).getCRealName());
                }
                if(oa.getCResult().equals("true")){
                    build.setResult("成功");
                }else{
                    build.setResult("失败");
                }
                logResps.add(build);
            });
            System.out.println("logResps: "+logResps);
            return logResps;
        }else{
            List<OaLog> collect = oaLogList.stream().filter(oaLog -> oaLog.getNUserId() == (userId)).collect(Collectors.toList());
            collect.forEach(oa -> {
                LogResp build = LogResp.builder()
                        .methodname(oa.getCMethodName())
                        .opname(oa.getCOpName())
                        .time(oa.getDtTime())
                        .build();
                if(userMapper.selectUserById(oa.getNUserId())!=null){
                    build.setUsername(userMapper.selectUserById(oa.getNUserId()).getCRealName());
                }
                if(oa.getCResult().equals("true")){
                    build.setResult("成功");
                }else{
                    build.setResult("失败");
                }
                logResps.add(build);
            });

        }
        return logResps;
    }
}
